package com.ruoyi.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.CalendarAddVO;
import com.ruoyi.common.core.domain.entity.CalendarContent;
import com.ruoyi.common.core.domain.entity.CalendarUpdateVO;
import com.ruoyi.common.core.domain.entity.CalendarVO;
import com.ruoyi.common.core.text.CharsetKit;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.ICalendarService;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/**
 * 参数配置 服务层实现
 *
 * @author ruoyi
 */
@Service
public class CalendarServiceImpl implements ICalendarService
{
    @Override
    public List<CalendarVO> selectConfigList(CalendarVO calendarVO) {
        List<CalendarVO> calendarVOList = getAll();
        if (StringUtils.isNotEmpty(calendarVO.getDate())) {
            calendarVOList = calendarVOList.stream().filter(a -> a.getDate().indexOf(calendarVO.getDate()) > -1).collect(Collectors.toList());
        }
        if (StringUtils.isNotEmpty(calendarVO.getSearchValue())) {
            String searchValue = calendarVO.getSearchValue();
            for(Iterator<CalendarVO> it = calendarVOList.iterator(); it.hasNext();){
                CalendarVO next = it.next();
                List<CalendarContent> matchList = next.getContentList().stream().filter(b -> b.getContent().indexOf(searchValue) > -1).collect(Collectors.toList());

                if(matchList.size() > 0){
                    next.setContentList(matchList);
                } else {
                    it.remove();
                }
            }
        }
        return calendarVOList;
    }

    @Override
    public List<CalendarVO> getAll() {
        String path = RuoYiConfig.getImportPath() + "\\data.json";
        String str = "";
        try {
            str = FileUtils.readFileToString(new File(path), CharsetKit.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONArray jsonArray = JSONArray.parseArray(str);
        CalendarVO[] calendarVOS = jsonArray.toArray(CalendarVO.class);
        return  new ArrayList<>(Arrays.asList(calendarVOS));
    }

    private void writeAll(List<CalendarVO> calendarVOList) {
        String path = RuoYiConfig.getImportPath() + "\\data.json";
        String jsonString = JSON.toJSONString(calendarVOList, "");
        try {
            FileUtils.writeStringToFile(new File(path), jsonString, CharsetKit.UTF_8, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int add(CalendarAddVO calendarAddVO) {
        try {
            List<CalendarVO> calendarVOList = getAll();
            String currentDate = calendarAddVO.getDate();
            AtomicBoolean isExist = new AtomicBoolean(false);
            calendarVOList.forEach(calendarVO -> {
                if (calendarVO.getDate().equals(currentDate)) {
                    isExist.set(true);
                    calendarVO.setType(calendarAddVO.getType());
                    CalendarContent calendarContent = new CalendarContent();
                    calendarContent.setContent(calendarAddVO.getContent());
                    calendarContent.setTime(calendarAddVO.getTime());
                    calendarContent.setId(IdUtils.fastUUID());
                    calendarVO.getContentList().add(calendarContent);
                    return;
                }
            });
            if (!isExist.get()) {
                CalendarVO calendarVO = new CalendarVO();
                calendarVO.setDate(currentDate);
                calendarVO.setType(calendarAddVO.getType());
                CalendarContent calendarContent = new CalendarContent();
                calendarContent.setContent(calendarAddVO.getContent());
                calendarContent.setTime(calendarAddVO.getTime());
                calendarContent.setId(IdUtils.fastUUID());
                calendarVO.getContentList().add(calendarContent);
                calendarVOList.add(calendarVO);
            }
            writeAll(calendarVOList);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public int update(CalendarUpdateVO calendarUpdateVO) {
        AtomicBoolean isExist = new AtomicBoolean(false);
        try {
            List<CalendarVO> calendarVOList = getAll();
            String currentId = calendarUpdateVO.getId();
            calendarVOList.forEach(calendarVO -> {
                calendarVO.getContentList().forEach(calendarContent -> {
                    if (currentId.equals(calendarContent.getId())) {
                        calendarContent.setContent(calendarUpdateVO.getContent());
                        calendarContent.setTime(calendarUpdateVO.getTime());
                        isExist.set(true);
                        return;
                    }
                });
            });
            writeAll(calendarVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return isExist.get() ? 1 : 0;
    }

    @Override
    public int deleteById(String id) {
        try {
            List<CalendarVO> calendarVOList = getAll();
            calendarVOList.forEach(calendarVO -> {
                List<CalendarContent> contentList = calendarVO.getContentList();
                Iterator<CalendarContent> iterator = contentList.iterator();
                while (iterator.hasNext()) {
                    CalendarContent calendarContent = iterator.next();
                    if (id.equals(calendarContent.getId())) {
                        iterator.remove();
                        break;
                    }
                }
            });

            Iterator<CalendarVO> iterator = calendarVOList.iterator();
            while (iterator.hasNext()) {
                CalendarVO next = iterator.next();
                if (next.getContentList().size() == 0) {
                    iterator.remove();
                    break;
                }
            }
            writeAll(calendarVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
}
